﻿namespace MicroCloud
{
    /// <summary>
    /// OrderedEnumerable集合扩展方法 
    /// </summary>
    public static class OrderedEnumerableExtensions
    {
        /// <summary>
        /// 将 <see cref="IOrderedQueryable{T}"/> 集合继续按指定字段排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public static IOrderedEnumerable<T> ThenBy<T>(this IOrderedEnumerable<T> source,
            string propertyName,
            ListSortDirection sortDirection = ListSortDirection.Ascending)
        {
            propertyName.CheckNotNullOrEmpty(nameof(propertyName));

            return CollectionPropertySorter<T>.ThenBy(source, propertyName, sortDirection);
        }

        /// <summary>
        /// 将 <see cref="IOrderedEnumerable{T}"/> 集合继续指定字段排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="sortCondition">列表字段排序条件</param>
        /// <returns></returns>
        public static IOrderedEnumerable<T> ThenBy<T>(this IOrderedEnumerable<T> source, SortCondition sortCondition)
        {
            sortCondition.CheckNotNull(nameof(sortCondition));

            return source.ThenBy(sortCondition.SortField, sortCondition.ListSortDirection);
        }
    }
}